package com.twu.biblioteca.controllers;


import com.twu.biblioteca.helpers.Encryption;
import com.twu.biblioteca.models.User;
import com.twu.biblioteca.models.UserDAO;

public final class ConsoleLogin extends Console {

    @Override
    public void run() {
        this.io.getOutput().append("Sign in\n\nLogin: ");
        System.out.println(this.io.getOutput());

        String number = this.io.getInput().nextLine();

        this.io.getOutput().delete(0, this.io.getOutput().length());
        this.io.getOutput().append("Password: ");
        System.out.println(this.io.getOutput());

        String password = this.io.getInput().nextLine();

        try {
            boolean logged = UserDAO.log(new User(number, new Encryption().encodeSHA(password), "Carlos", "jcsouza@thoughtworks.com", "95873448"));

            this.io.getOutput().delete(0, this.io.getOutput().length());

            if(logged){
                this.io.getOutput().delete(0, this.io.getOutput().length());
                new ConsoleMainMenu().run();
            }
            else{
                System.out.println("\nUser and password not found\n\n");

                new ConsoleLogin().run();
            }
        }
        catch (IllegalArgumentException e){
            System.out.println("\nUser and password not found\n\n");

            new ConsoleLogin().run();
        }
    }
}
